package com.example.my_self_view

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.widget.TextView

class ColorTrackTextView: androidx.appcompat.widget.AppCompatTextView {

    // 实现一个文字两种颜色 - 绘制不变色字体的画笔
    private val mOriginPaint by lazy { Paint() }
    // 实现一个文字两种颜色 - 绘制变色字体的画笔
    private val mChangePaint by lazy { Paint() }
    // 当前进度
    private var mCurrentProgress = 0.0f
    // 实现不同朝向
    private var mDirection = Direction.LEFT_TO_RIGHT

    enum class Direction{
        LEFT_TO_RIGHT, RIGHT_TO_LEFT
    }

    constructor(context: Context): this(context, null){}

    constructor(context: Context, attrs: AttributeSet?): this(context, attrs, 0){}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr){
        initData(attrs)
    }

    private fun initData(attrs: AttributeSet?) {
        val array = context.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView)
        array.apply {
            try {
                val originColor = array.getColor(R.styleable.ColorTrackTextView_originColor, Color.BLACK)
                val changeColor = array.getColor(R.styleable.ColorTrackTextView_changeColor, Color.RED)

                setPaintByColor(mOriginPaint, originColor)
                setPaintByColor(mChangePaint, changeColor)
            } finally {
                recycle()
            }
        }
    }

    private fun setPaintByColor(paint: Paint, color: Int){
        paint.apply {
            this.color = color
            this.isAntiAlias = true
            this.isDither = true //防抖动
            this.textSize = this@ColorTrackTextView.textSize
        }
    }

    // 1.一个文字两种颜色
    // 利用canvas.clipRect()的API  可以裁剪  左边用一个画笔去画，右边用另一个画笔去画
    override fun onDraw(canvas: Canvas) {

//        canvas.clipRect()  裁剪区域

        // 根据进度把中间值算出来
        val middle = width * mCurrentProgress
        if (mDirection == Direction.LEFT_TO_RIGHT) {
            drawText(canvas, mChangePaint, 0, middle.toInt())
            // 绘制原色
            drawText(canvas, mOriginPaint, middle.toInt(), width)
        } else {
            drawText(canvas, mChangePaint, width - middle.toInt(), width)
            // 绘制原色
            drawText(canvas, mOriginPaint, 0, width - middle.toInt())
        }
    }

    private fun drawText(canvas: Canvas, paint: Paint, start: Int, end: Int) {
        canvas.save()
        canvas.clipRect(start,0,end,height)
        // 获取字体的宽度
        val bounds = Rect()
        paint.getTextBounds(text.toString(), 0, text.length, bounds)
        val dx = width / 2 - bounds.width() / 2
        // 基线
        val fontMetricsInt = paint.fontMetricsInt
        val dy = - (fontMetricsInt.top + fontMetricsInt.bottom) / 2
        val baseLine = height / 2 + dy
        canvas.drawText(text.toString(), dx.toFloat(), baseLine.toFloat(), paint)
        canvas.restore()
    }

    fun setDirection(direction: Direction) {
        this.mDirection = direction
    }

    fun setCurrentProgress(currentProgress: Float) {
        this.mCurrentProgress = currentProgress
        invalidate()
    }

    fun setChangeColor(color: Int) {
        this.mChangePaint.color = color
    }

    fun setOriginColor(color: Int) {
        this.mOriginPaint.color = color
    }
}